using System;
using System.Threading;
using System.Threading.Tasks;
using MtApi5;

namespace MT5Trade
{
    public class SimulateAppFlow
    {
        private static MtApi5Client MtClientMain;
        private static MtApi5Client MtClientFollow;
        private static bool IsStartMain = false;
        private static bool IsStartFollow = false;
        private static int portMain = 8228;
        private static int portFollow = 8233;

        public static void Main(string[] args)
        {
            Console.WriteLine("=== 模拟应用程序流程测试 ===");
            Console.WriteLine($"开始时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");

            // 模拟 MtTrade5_Load
            Console.WriteLine("\n[1] 模拟窗体加载...");
            MtTrade5_Load();

            // 模拟点击主账号连接按钮
            Console.WriteLine("\n[2] 模拟点击主账号连接按钮...");
            btnMain_Click();

            // 等待主账号连接
            Thread.Sleep(5000);

            // 模拟点击从账号连接按钮
            Console.WriteLine("\n[3] 模拟点击从账号连接按钮...");
            btnFollow_Click();

            // 等待观察结果
            Thread.Sleep(10000);

            // 检查最终状态
            Console.WriteLine("\n[4] 最终状态检查:");
            Console.WriteLine($"主账号状态: {MtClientMain?.ConnectionState}");
            Console.WriteLine($"从账号状态: {MtClientFollow?.ConnectionState}");

            if (MtClientMain?.ConnectionState == Mt5ConnectionState.Connected &&
                MtClientFollow?.ConnectionState == Mt5ConnectionState.Connected)
            {
                Console.WriteLine("\n✅✅ 两个账号都连接成功！");
            }
            else
            {
                Console.WriteLine("\n⚠️ 有问题！");
            }

            Console.WriteLine("\n测试结束，按任意键退出...");
            Console.ReadLine();

            // 停止循环
            IsStartMain = false;
            IsStartFollow = false;
        }

        private static void MtTrade5_Load()
        {
            try
            {
                MtClientMain = new MtApi5Client();
                MtClientMain.ConnectionStateChanged += MainApiClient_ConnectionStateChanged;
                Console.WriteLine("  ✓ MtClientMain 已创建");

                MtClientFollow = new MtApi5Client();
                MtClientFollow.ConnectionStateChanged += FollowApiClient_ConnectionStateChanged;
                Console.WriteLine("  ✓ MtClientFollow 已创建");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  ❌ 初始化失败: {ex.Message}");
            }
        }

        private static void btnMain_Click()
        {
            if (!IsStartMain)
            {
                Console.WriteLine($"  启动主账号连接任务，端口: {portMain}");
                Task.Run(() =>
                {
                    IsStartMain = true;
                    while (IsStartMain)
                    {
                        ConnectMain();
                        Thread.Sleep(5000);
                    }
                });
            }
        }

        private static void btnFollow_Click()
        {
            if (!IsStartFollow)
            {
                Console.WriteLine($"  启动从账号连接任务，端口: {portFollow}");
                Task.Run(() =>
                {
                    IsStartFollow = true;
                    while (IsStartFollow)
                    {
                        ConnectFollow();
                        Thread.Sleep(5000);
                    }
                });
            }
        }

        private static void ConnectMain()
        {
            try
            {
                if (MtClientMain == null)
                {
                    Console.WriteLine("  错误: MtClientMain未初始化！");
                    MtClientMain = new MtApi5Client();
                    MtClientMain.ConnectionStateChanged += MainApiClient_ConnectionStateChanged;
                }

                if (MtClientMain.ConnectionState == Mt5ConnectionState.Connected)
                {
                    // 静默返回
                    return;
                }

                Console.WriteLine($"  主账号未连接，开始连接到端口 {portMain}...");
                MtClientMain.BeginConnect(portMain);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  ConnectMain错误: {ex.Message}");
            }
        }

        private static void ConnectFollow()
        {
            try
            {
                if (MtClientFollow == null)
                {
                    Console.WriteLine("  错误: MtClientFollow未初始化！");
                    MtClientFollow = new MtApi5Client();
                    MtClientFollow.ConnectionStateChanged += FollowApiClient_ConnectionStateChanged;
                }

                if (MtClientFollow.ConnectionState == Mt5ConnectionState.Connected)
                {
                    // 静默返回
                    return;
                }

                Console.WriteLine($"  从账号未连接，开始连接到端口 {portFollow}...");
                MtClientFollow.BeginConnect(portFollow);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  ConnectFollow错误: {ex.Message}");
            }
        }

        private static void MainApiClient_ConnectionStateChanged(object sender, Mt5ConnectionEventArgs e)
        {
            Console.WriteLine($"  [主账号] 状态变化: {e.Status}");
        }

        private static void FollowApiClient_ConnectionStateChanged(object sender, Mt5ConnectionEventArgs e)
        {
            Console.WriteLine($"  [从账号] 状态变化: {e.Status}");
        }
    }
}